﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SummonExplodingPet : TargetSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (myCreatureBase is Character)
            {
                Character character = (Character)myCreatureBase;
                if (character.petManager.NumPets() > 3)
                {
                    Message message = new Message();
                    message.Type = "server";
                    message.CreatureID = character.CreatureID;
                    message.Text = "You have to many pets";

                    character.PlaySound("Ding", character.MapX, character.MapY);
                    character.AddMessage(message);

                    return;
                }
                List<Pet_Controller> pets = character.petManager.GetPets();
                foreach (Pet_Controller pet in pets)
                {
                    if (pet.MyCreature.ImageURL == "LavaBomb")
                    {
                        Message message = new Message();
                        message.Type = "server";
                        message.CreatureID = character.CreatureID;
                        message.Text = "You can not have more than 1 Lava Bomb";

                        character.PlaySound("Ding", character.MapX, character.MapY);
                        character.AddMessage(message);

                        return;
                    }
                }
            }

            MapItem currentMapItem = null;
            foreach (MapItem mapItem in MapItemsWithin12)
            {
                if (mapItem.MapX == myCreatureBase.MapX && mapItem.MapY == myCreatureBase.MapY)
                {
                    currentMapItem = mapItem;
                    break;
                }
            }

            DeathEffectCreature explodingPet = new DeathEffectCreature(myCreatureBase.GameHandler);
            CreatureBase ig = (CreatureBase)explodingPet;
            explodingPet.petController = new Pet_Controller(ig, myCreatureBase, currentMapItem);

            explodingPet.EffectCastLevel = CastLevel - 2;
            explodingPet.EffectCasterCharID = myCreatureBase is Character ? myCreatureBase.ID : -1;
            explodingPet.EffectCasterInt = myCreatureBase.IntelligenceTotal;
            explodingPet.EffectCasterWis = myCreatureBase.WisdomTotal;

            explodingPet.MaxHPBase = myCreatureBase.HP;
            explodingPet.HP = myCreatureBase.HP;

            explodingPet.MaxStaminaBase = 1000;
            explodingPet.Stamina = 1000;

            explodingPet.MaxManaBase = 0;
            explodingPet.Mana = 0;

            explodingPet.Armor = 0;
            explodingPet.Agility = 0;
            explodingPet.ResistFire = 10000;

            explodingPet.Dexterity = 0;
            explodingPet.Strength = 0;
            explodingPet.Wisdom = 0;
            explodingPet.Intelligence = 0;

            explodingPet.Defense = 1;
            explodingPet.DefenseSkill = 1;
            explodingPet.HandBlockWeapon = "flame";

            explodingPet.RushAttack = false;
            explodingPet.Water = true;
            explodingPet.Air = false;
            explodingPet.Land = true;

            CreatureAttackWeapon iceGolemWeapon = new CreatureAttackWeapon();
            iceGolemWeapon.Accuracy = 100;
            iceGolemWeapon.Description = "burns {name}";
            iceGolemWeapon.DmgHigh = 20;
            iceGolemWeapon.DmgLow = 10;
            iceGolemWeapon.Frequency = 1;
            iceGolemWeapon.Sequence = 1;
            iceGolemWeapon.SkillLevel = CastLevel / 2;
            iceGolemWeapon.Speed = 3;

            explodingPet.attackSequenceManager.Attacks.Add(iceGolemWeapon);

            explodingPet.Name = myCreatureBase.Name + "'s Lava Bomb";
            explodingPet.SoundDeath = "SpiderDeath";

            //No corpse image will cause DropLoot() to not generate a corpse
            explodingPet.CorpseImageURL = "";

            explodingPet.CreatureID = myCreatureBase.GameHandler.ItemIDGen.GetCreatureUID();
            explodingPet.CreatureType = "Creature";
            explodingPet.Faction = myCreatureBase.Faction;
            explodingPet.ID = -1;
            explodingPet.ImageURL = "LavaBomb";
            explodingPet.Instance = myCreatureBase.Instance;
            explodingPet.MapID = myCreatureBase.MapID;

            explodingPet.MaxMoves = 4;

            explodingPet.Timer_Regen.Start();
            explodingPet.creatureAI.Timer_CreatureRound.Start();

            MapItem mapitemToUpdate = myCreatureBase.currentMapItem;
            List<Character> CharsToUpdate = new List<Character>();
            foreach (MapItem mapitem in myCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharsToUpdate.Add((Character)creaturebase);
                }
                if (mapitem.MapX == myCreatureBase.MapX && mapitem.MapY == myCreatureBase.MapY)
                {
                    explodingPet.SetPosition(mapitem);
                    mapitem.CreaturesBases.Add(explodingPet);
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                //character.PlaySound(soundToPlay.Name, soundToPlay.MapX, soundToPlay.MapY);
                character.AddMapItem(mapitemToUpdate);

                if (character == myCreatureBase)
                    character.petManager.AddPet(explodingPet.petController);
            }
        }
    }
}
